namespace IMPlant.Core.Web.Control.GridViewFunction
{
    using IMPlant.Core.Web.Control;
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class SmartSortingFunction : ExtendFunction
    {
        public SmartSortingFunction()
        {
        }

        public SmartSortingFunction(IMPlant.Core.Web.Control.GridView grid) : base(grid)
        {
        }

        private void _grid_RowDataBoundCell(object sender, GridViewTableCell gvtc)
        {
            if ((!string.IsNullOrEmpty(base._grid.SortExpression) && base._grid.SmartSorting.AllowSortTip) && (gvtc.RowType == DataControlRowType.Header))
            {
                this.DisplaySortTip(base._grid.SortExpression, gvtc.TableCell);
            }
        }

        private void _grid_Sorting(object sender, GridViewSortEventArgs e)
        {
            if (base._grid.SmartSorting.AllowMultiSorting)
            {
                e.SortExpression = this.GetSortExpression(e);
            }
        }

        protected void DisplaySortTip(string sortExpression, TableCell tc)
        {
            string[] array = sortExpression.Split(new char[] { ',' });
            if ((tc.Controls.Count > 0) && (tc.Controls[0] is LinkButton))
            {
                string columnName = ((LinkButton) tc.Controls[0]).CommandArgument;
                int index = Array.FindIndex<string>(array, delegate (string s) {
                    return string.Compare(s, columnName) == 0;
                });
                if (index != -1)
                {
                    SortDirection ascending = SortDirection.Ascending;
                    if (base._grid.SmartSorting.AllowMultiSorting && (array[index].IndexOf(DefaultSorting.DESC.ToString()) != -1))
                    {
                        ascending = SortDirection.Descending;
                    }
                    else if (base._grid.SmartSorting.AllowMultiSorting && (array[index].IndexOf(DefaultSorting.ASC.ToString()) != -1))
                    {
                        ascending = SortDirection.Ascending;
                    }
                    else if (!base._grid.SmartSorting.AllowMultiSorting && (base._grid.SortDirection == SortDirection.Descending))
                    {
                        ascending = SortDirection.Descending;
                    }
                    string sortAscImageUrl = null;
                    string text = null;
                    if (ascending == SortDirection.Ascending)
                    {
                        text = base._grid.SmartSorting.SortAscText;
                        sortAscImageUrl = base._grid.SmartSorting.SortAscImageUrl;
                        if (string.IsNullOrEmpty(sortAscImageUrl))
                        {
                            sortAscImageUrl = base._grid.Page.ClientScript.GetWebResourceUrl(base.GetType(), "IMPlant.Core.Web.UI.Image.Asc.gif");
                        }
                    }
                    else
                    {
                        text = base._grid.SmartSorting.SortDescText;
                        sortAscImageUrl = base._grid.SmartSorting.SortDescImageUrl;
                        if (string.IsNullOrEmpty(sortAscImageUrl))
                        {
                            sortAscImageUrl = base._grid.Page.ClientScript.GetWebResourceUrl(base.GetType(), "IMPlant.Core.Web.UI.Image.Desc.gif");
                        }
                    }
                    Image child = new Image();
                    child.ImageUrl = sortAscImageUrl;
                    tc.Controls.Add(child);
                    tc.Controls.Add(new LiteralControl(text));
                    if (base._grid.SmartSorting.AllowMultiSorting)
                    {
                        int num2 = index + 1;
                        tc.Controls.Add(new LiteralControl(num2.ToString()));
                    }
                }
            }
        }

        protected override void Execute()
        {
            base._grid.Sorting += new GridViewSortEventHandler(this._grid_Sorting);
            base._grid.RowDataBoundCell += new IMPlant.Core.Web.Control.GridView.RowDataBoundCellHandler(this._grid_RowDataBoundCell);
        }

        protected string GetSortExpression(GridViewSortEventArgs e)
        {
            string[] sortColumns = null;
            string sortExpression = base._grid.SortExpression;
            if (!string.IsNullOrEmpty(sortExpression))
            {
                sortColumns = sortExpression.Split(new char[] { ',' });
            }
            if (sortExpression.IndexOf(e.SortExpression) != -1)
            {
                sortExpression = this.ModifySortExpression(sortColumns, e.SortExpression);
            }
            else
            {
                sortExpression = sortExpression + ("," + e.SortExpression + string.Format(" {0}", base._grid.SmartSorting.DefaultSorting.ToString()));
            }
            return sortExpression.Trim(new char[] { ',' });
        }

        protected string ModifySortExpression(string[] sortColumns, string sortExpression)
        {
            string str = sortExpression + string.Format(" {0}", DefaultSorting.ASC.ToString());
            string str2 = sortExpression + string.Format(" {0}", DefaultSorting.DESC.ToString());
            string str3 = base._grid.SmartSorting.DefaultSorting.ToString().ToUpper();
            for (int i = 0; i < sortColumns.Length; i++)
            {
                if (str.Equals(sortColumns[i], StringComparison.OrdinalIgnoreCase) && str3.Equals(DefaultSorting.ASC.ToString()))
                {
                    sortColumns[i] = str2;
                    break;
                }
                if (str.Equals(sortColumns[i], StringComparison.OrdinalIgnoreCase) && !str3.Equals(DefaultSorting.ASC.ToString()))
                {
                    Array.Clear(sortColumns, i, 1);
                    break;
                }
                if (str2.Equals(sortColumns[i], StringComparison.OrdinalIgnoreCase) && str3.Equals(DefaultSorting.DESC.ToString()))
                {
                    sortColumns[i] = str;
                }
                else if (str2.Equals(sortColumns[i], StringComparison.OrdinalIgnoreCase) && !str3.Equals(DefaultSorting.DESC.ToString()))
                {
                    Array.Clear(sortColumns, i, 1);
                    break;
                }
            }
            return string.Join(",", sortColumns).Replace(",,", ",").Trim(new char[] { ',' });
        }
    }
}

